package com.www.framework.task;

import com.www.common.utils.DateUtils;
import com.www.project.meetschool.domain.MeetEvent;
import com.www.project.meetschool.mapper.MeetEventMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.www.common.utils.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author www
 */
@Configuration
@EnableScheduling
public class RyTask
{
    @Autowired
    private MeetEventMapper meetEventMapper;

    @Scheduled(fixedRate=500000)
    public void checkStatus()
    {
        MeetEvent event = new MeetEvent();
        List<MeetEvent> events = meetEventMapper.selectEventList(event);
        Long now = new Date().getTime();
        for (MeetEvent event1:events) {
            if(!"已结束".equals(event1.getStatus())) {
                if(StringUtils.isNotEmpty(event1.getEventStartTime()) && StringUtils.isNotEmpty(event1.getEventEndTime())){
                    if (DateUtils.parseDate(event1.getEventStartTime()).getTime() <= now && now <= DateUtils.parseDate(event1.getEventEndTime()).getTime()) {
                        event1.setStatus("进行中");
                    }
                }
                if (StringUtils.isNotEmpty(event1.getEventEndTime()) && DateUtils.parseDate(event1.getEventEndTime()).getTime() <= now) {
                    event1.setStatus("已结束");
                }
            }else{
                if(StringUtils.isNotEmpty(event1.getEventEndTime()) && (DateUtils.parseDate(event1.getEventEndTime()).getTime()-now>=7*DateUtils.MILLIS_PER_DAY)){
                    event1.setDelFlag(true);
                }
            }
            meetEventMapper.updateEvent(event1);
        }


    }

}
